package fr.upmc.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import fr.upmc.bean.Contact;
import fr.upmc.bean.ContactGroup;

public class ContactDAO extends DAO<Contact>{

	public ContactDAO(Session session) {
		super(session);
	}

	@Override
	public long create(Contact obj) {
		Transaction transaction = null;
		Long contactId = null;

		try {
			transaction = session.beginTransaction();
			contactId = (Long)session.save(obj);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		}

		System.out.println("Contact " + contactId + " ajouté !");

		return (long)contactId;
	}

	@Override
	public Contact read(long id) {

		Transaction transaction = null;
		Contact contact = null;

		String q = "select contact from Contact contact " +
				   "join contact.profiles profiles " +
				   "join contact.books books " +
				   "where contact.contactId = " + id;
		
		try {
			transaction = session.beginTransaction();
			
			contact = (Contact)session.createQuery(q).uniqueResult();
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		}

		return contact;
	}
	
	public Contact read(String name) {

		Transaction transaction = null;
		Contact contact = null;

		try {
			transaction = session.beginTransaction();
			contact = (Contact)session.createCriteria(Contact.class)
                    .add(Restrictions.eq("firstName", name))
                    .uniqueResult();
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		}

		return contact;
	}

	@Override
	public boolean update(Contact obj) {

		Transaction transaction = null;
		Contact contact = null;

		try {
			transaction = session.beginTransaction();

			if(session.get(Contact.class, obj.getContactId()) != null){
				/*contact = (Contact)session.load(Contact.class, obj.getContactId());
				contact.setFirstName(obj.getFirstName());
				contact.setLastName(obj.getLastName());
				contact.setEmail(obj.getEmail());*/

				session.saveOrUpdate(obj);
			}
			else{
				transaction.commit();
				return false;
			}

			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		}

		System.out.println("Contact " + obj.getContactId() + " mit a jour !");

		return true;
	}

	@Override
	public boolean delete(long id) {

		Transaction transaction = null;
		Contact contact = null;

		try {
			transaction = session.beginTransaction();

			if(session.get(Contact.class, id) != null){
				contact = (Contact)session.load(Contact.class, id);
				session.delete(contact);
			}
			else{
				transaction.commit();
				return false;
			}
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		}
		
		return true;
	}
	
	public List<Contact> getAllContacts(){
		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();
			List<Contact> list = session.createCriteria(Contact.class)
					.list();
			return list;
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}

		return null;
	}
}
